package cn.cmcc.intertelecom.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmcc.intertelecom.bean.Power;
import cn.cmcc.intertelecom.bean.PowerGroup;
import cn.cmcc.intertelecom.bean.PowerGroupExample;
import cn.cmcc.intertelecom.bean.PowerGroupExample.Criteria;
import cn.cmcc.intertelecom.bean.Role;
import cn.cmcc.intertelecom.bean.RoleExample;
import cn.cmcc.intertelecom.bean.RoleGroup;
import cn.cmcc.intertelecom.dao.PowerGroupMapper;
import cn.cmcc.intertelecom.dao.PowerMapper;
import cn.cmcc.intertelecom.dao.RoleMapper;

@Service
public class RoleService {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired PowerGroupMapper powerGroupMapper;

	/**
	 * 获取所有的 角色 包括角色的模块
	 * @return
	 */
	public List<Role> getAllWithModule() {
		
		RoleExample example=new RoleExample();
		example.setOrderByClause("id");
		return roleMapper.selectByExampleWithModule(example);
	}

	/**
	 * 将角色插入到数据可
	 * @param role
	 */
	public void insertRole(Role role) {
		roleMapper.insert(role);
		
	}

	public void updateRole(RoleGroup roleGroup) {
		//roleMapper
		
	}
	
	/**
	 * 根据角色的id 获取该角色 以及该角色所对应的权限
	 * 
	 * (需要修改)
	 * @param roleId
	 * @return
	 */
	public Role getRoleWithPowersById(Integer roleId) {
		
		Role role = roleMapper.selectByPrimaryKey(roleId);
		PowerGroupExample example=new PowerGroupExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		List<PowerGroup> list = powerGroupMapper.selectByExample(example);
		
		String[] powers =new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			powers[i]=list.get(i).getPower();
		}
		role.setPowers(powers);
		return  role;
	}

	/**
	 * 仅仅获取所有的角色
	 * @return
	 */
	public List<Role> getAllRole() {
		
		return roleMapper.selectByExample(null);
	}

	/**
	 * 删除一条 记录
	 * @param parseInt
	 */
	public void deleteDept(int id) {
		
		roleMapper.deleteByPrimaryKey(id);
		
	}


	/**
	 * 批量删除多条记录
	 * @param ids
	 */
	public void deleteBatch(List<Integer> ids) {
		
		
		RoleExample example=new RoleExample();
		cn.cmcc.intertelecom.bean.RoleExample.Criteria criteria = example.createCriteria();
		criteria.andIdIn(ids);
		roleMapper.deleteByExample(example);
		
	}
}
